package pl.leader100.rest;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello

public class RetrieveDocHTML extends RetrieveDoc {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String locale;
	String course;
	String path;
	String mimeType;

	public RetrieveDocHTML(UriInfo uriInfo, Request request, String locale,
			String course, String path, String mimeType) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.locale = locale;
		this.course = course;
		this.path = path;
		this.mimeType = mimeType;
	}

	public String getPath() {
		File rootFolder = File.listRoots()[0];
		String root = rootFolder.getAbsolutePath();
		char sep = File.separatorChar;
		String path = root + sep + "files" + sep + "pl" + sep
				+ this.course + sep;
		return path;
	}
	
	// This method is called if TEXT_PLAIN is request
	/*
	 * @GET
	 * 
	 * @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() {
	 * return "no way"; }
	 */
	// Application integration
	

	@GET
	@Produces({ "text/html" })
	public StreamingOutput retrieveDocPDF() throws Exception {

		return new StreamingOutput() {

			@Override
			public void write(OutputStream output) throws IOException,
					WebApplicationException {

				final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
				
				char sep = File.separatorChar;
				String sep2 = String.valueOf(sep)+String.valueOf(sep);
				
				String os = System.getProperty("os.name");
				String root;
				if (os.toLowerCase().indexOf("win") >= 0) {
					root = "c:\\";
				}
				else 
					root = "//";
				
				String filePath = root+"files" + sep2 + locale + sep2 + course;
				//String filePath = getPath();
				
				File file = new File(filePath + sep2 + path);
				// System.err.println("filepath: "+filePath);
				/*
				 * if (!file.exists()) { try { String file_name =
				 * filePath+"//test.txt"; FileWriter f = new
				 * FileWriter(file_name); BufferedWriter out = new
				 * BufferedWriter (f); String text =
				 * "This is goint to be written to the file"; out.write(text);
				 * out.close(); } catch (IOException e) {
				 * System.out.println(e.getMessage()); }
				 * //output.write("<h1>Error 404</h1>".getBytes()); return; }
				 */

				// Prepare streams.
				BufferedInputStream input = null;
				// System.err.println("pass par la");
				try {
					// Open streams.
					// System.err.println(file);
					input = new BufferedInputStream(new FileInputStream(file),
							DEFAULT_BUFFER_SIZE);

					// Write file contents to response.
					byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
					int length;
					while ((length = input.read(buffer)) > 0) {
						output.write(buffer, 0, length);
					}
				} finally {
					// Gently close streams.
				}

			}
		};
	}
	// Application integration
	/*
	 * @GET
	 * 
	 * @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public
	 * StreamingOutput getDocXml() throws Exception {
	 * 
	 * return new StreamingOutput() {
	 * 
	 * @Override public void write(OutputStream output) throws IOException,
	 * WebApplicationException {
	 * 
	 * final int DEFAULT_BUFFER_SIZE = 10240; // 10KB. String filePath =
	 * "files";
	 * 
	 * File file = new File(filePath+"/"+path); if (!file.exists()) return;
	 * 
	 * // Prepare streams. BufferedInputStream input = null;
	 * 
	 * try { // Open streams. input = new BufferedInputStream(new
	 * FileInputStream(file), DEFAULT_BUFFER_SIZE);
	 * 
	 * // Write file contents to response. byte[] buffer = new
	 * byte[DEFAULT_BUFFER_SIZE]; int length; while ((length =
	 * input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally {
	 * // Gently close streams. }
	 * 
	 * } }; }
	 */

	// For the browser
	/*
	 * @GET
	 * 
	 * @Produces(MediaType.TEXT_XML) public Doc getTodoHTML() { Doc doc =
	 * DocDao.GetInstance().getModel().get(path); if(doc==null) throw new
	 * RuntimeException("Get: Doc with " + path+ " not found"); return doc; }
	 */

}
